package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishWithFlavor;
import com.sky.service.Dishservice;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@Service
public class Dishservicelml implements Dishservice {
    @Autowired
    private DishMapper mapper;
    @Autowired
    private DishWithFlavor flavor;

    @Override
    public int insertdish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        int count = mapper.insertdish(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors.size() > 0 && flavors != null) {
            for (DishFlavor dishFlavor : flavors) {
                dishFlavor.setDishId(id);
            }
            int count2 = flavor.insertdishwithflavors(flavors);
        }
        return count;
    }

    @Override
    public Page<Dish> selectwithpage(DishPageQueryDTO queryDTO) {
        Page<Dish> dishes = (Page<Dish>) mapper.selectwithpage(queryDTO);
        return dishes;
    }

    @Override
    public String deletebyids(List<Long> ids) {
        int count = 0;
        Integer status = null;
        //没有起售的商品可以删除
        for (Long id : ids) {
            Dish dish = mapper.selectbyid(id);
            log.info("dish is {}", dish);
            status = dish.getStatus();
            log.info("status is {}", status);
            try {
                if (status == 0) {

                    //没有被套餐关联的商品可以删除
                    SetmealDish setmeal = mapper.setmealselectbyid(id);
                    if (setmeal == null)
                        //可以删除
                        mapper.deletebyid(id);
                    //删除菜品后，关联的口味数据也要删掉
                    mapper.deleteflavorbyid(id);
                }
            } catch (Exception e) {
                log.error("删除菜品失败", e);
                return "删除菜品失败";
            }
            if (status == 1)
                return "不可删除";
        }
        return "删除成功";
    }

    @Override
    public DishDTO selectDish(Long id) {
        Dish dish = mapper.selectbyid(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish, dishDTO);
        List<DishFlavor> flavors = mapper.selectflavorbyid(id);
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    @Override
    public Boolean updatedish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        log.info("dto is{}", dto);
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dto.getId());
        }
        int count1 = 0;
        int count2 = 0;
        if (flavors != null && flavors.size() > 0) {
            //删除口味
            count1 = mapper.deleteflavorbyid(dto.getId());
            //插入口味数据
            count2 = mapper.insertflavor(flavors);
        }
        //更新dish
        int count = mapper.updatedish(dish);
        if (count != 0 || count1 != 0 || count2 != 0)
            return true;
        return false;
    }

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = mapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = mapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
